﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Security;
using System.Security.Cryptography;

namespace org.alex.edss.Security
{
    public class DES
    {
        private static DES des = new DES();

        public static DES GetTransfer()
        {
            return des;
        }

        public byte[] IV
        {
            get;
            set;
        }

        private DESCryptoServiceProvider Transfer;

        private DES()
        {
            IV = new byte[] { 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF };
            Transfer = new DESCryptoServiceProvider();
        }

        public byte[] Encrypto(byte[] InputData,string Key)
        {
            if (Key.Length < 8)
            {
                throw new ArgumentException("Key can not be less than 8");
            }
            byte[] cKey=Encoding.UTF8.GetBytes(Key.Substring(0,8));
            try
            {
                ICryptoTransform ent = Transfer.CreateEncryptor(cKey, IV);
                return ent.TransformFinalBlock(InputData, 0, InputData.Length);
            }
            catch (Exception ex)
            {
                throw new Exception("error when Crypto", ex);
            }
        }
        public byte[] Decrypto(byte[] InputData,string Key)
        {
            if (Key.Length < 8)
            {
                throw new ArgumentException("Key can not be less than 8");
            }
            byte[] cKey = Encoding.UTF8.GetBytes(Key.Substring(0, 8));
            try
            {
                ICryptoTransform ent = Transfer.CreateDecryptor(cKey, IV);
                return ent.TransformFinalBlock(InputData, 0, InputData.Length);
            }
            catch (Exception ex)
            {
                throw new Exception("error when Crypto", ex);
            }
        }


    }

    
}
